#!/usr/bin/env bash

include "libs/regex.sh"

#help.cmd config@config : 配置ohos-tool
#alias.cmd occ : config@config
dynamic_cmd:config:config()
{
    database:global_load
    use_docker="$(database:global_get use_docker)"
    use_window="$(database:global_get use_window)"
    git_update="$(database:global_get git_update)"
    if zenity:list \
        --width=500 \
        --height=200 \
        --text="配置ohos-tool" \
        --title="配置ohos-tool" \
        --columns="是否启用:特性:描述" \
        --keys="use_docker:use_window:git_update" \
        --others="用docker编译:用窗口提醒耗时操作完成:git仓库自动更新"; then
        database:global_set use_docker "$use_docker"
        database:global_set use_window "$use_window"
        database:global_set git_update "$git_update"
        database:global_save
    fi
}

# config@repo.option --add [path] : 添加仓库
dynamic_cmd:config:repo--add()
{
    OHOS_CONFIG_REPO="add"
}

# config@repo.option --del : 删除仓库
dynamic_cmd:config:repo--del()
{
    OHOS_CONFIG_REPO="del"
}

# config@repo.option --product [PRODUCT] : 设置默认产品
dynamic_cmd_config_repo_product_need="false"
dynamic_cmd:config:repo--product()
{
    OHOS_CONFIG_REPO="set"
    dynamic_cmd_config_repo_product_need="true"
    if arguments:is_option; then
        dynamic_cmd_config_repo_product=""
    else
        dynamic_cmd_config_repo_product="$(arguments:get_now)"
    fi
}

#help.cmd config@repo [reponame] : 仓库动作，默认列出仓库
#alias.cmd olr : config@repo
dynamic_cmd:config:repo()
{
    case "$OHOS_CONFIG_REPO" in
        add)
            arguments $*
            local repo_name repo_path
            if arguments:have_next; then
                repo_name="$(arguments:get_now)"
                arguments:next
            else
                repo_name="$(zenity:input_string "请输入仓库名(由字母、数字和下划线组成，字母打头)" "repo_name")"
                if [ "$repo_name" == "" ]; then
                    echo "添加仓库中断"
                    return
                fi
            fi

            if ! [[ "$repo_name" =~ ($regex_id) ]]; then
                echo "所输入的仓库名不符合标识符规则(字母打头，字母数字若干)"
                return 1
            fi

            for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                if [ "$(database:global_array_get repo_name $i)" == "$repo_name" ]; then
                    echo "已经存在同名仓库"
                    return 1
                fi
            done

            if arguments:have_next; then
                repo_path="$(arguments:get_now)"
                arguments:next
            else
                repo_path="$(zenity:dir_selector "请选择${repo_name}仓库.repo所在的文件夹")"
                if [ "$repo_path" == "" ]; then
                    echo "添加仓库中断"
                    return 1
                fi
            fi
            arguments:destroy

            local repo_number=$(database:global_get repo_number)
            database:global_inc repo_number
            database:global_array_set repo_name $repo_number $repo_name
            database:global_array_set repo_path $repo_number $repo_path
            database:global_array_set repo_product $repo_number Hi3516DV300
            database:global_save
            ;;
        del)
            arguments $*
            local deleting_repo_name
            if arguments:have_next; then
                deleting_repo_name="$(arguments:get_now)"
                arguments:next
            else
                local repos=()
                for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                    repos+=($(database:global_array_get repo_name $i))
                done

                deleting_repo_name=$(zenity:list_selector "仓库名称" ${repos[*]})
                if [ "$deleting_repo_name" == "" ]; then
                    echo "删除仓库中断"
                    return 1
                fi
            fi

            if ! [[ "$deleting_repo_name" =~ ($regex_id) ]]; then
                echo "所输入的仓库名不符合标识符规则(字母打头，字母数字若干)"
                return 1
            fi

            for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                if [ "$(database:global_array_get repo_name $i)" == "$deleting_repo_name" ]; then
                    database:global_dec repo_number
                    for (( j = $i; j < $(database:global_get repo_number); j++ )); do
                        local repo_name=$(database:global_array_get repo_name $((j + 1)))
                        local repo_path=$(database:global_array_get repo_path $((j + 1)))
                        local repo_product=$(database:global_array_get repo_product $((j + 1)))
                        database:global_array_set repo_name $j $repo_name
                        database:global_array_set repo_path $j $repo_path
                        database:global_array_set repo_product $j $repo_product
                    done
                    database:global_save
                    return 0
                fi
            done
            echo "找不到要删除的仓库: $deleting_repo_name"
            return 1
            ;;
        set)
            arguments $*
            local repo_name
            if arguments:have_next; then
                repo_name="$(arguments:get_now)"
                arguments:next
            else
                local repos=()
                for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                    repos+=($(database:global_array_get repo_name $i))
                done

                repo_name=$(zenity:list_selector "仓库名称" ${repos[*]})
                if [ "$repo_name" == "" ]; then
                    echo "设置仓库默认产品中断"
                    return 1
                fi
            fi

            if ! [[ "$repo_name" =~ ($regex_id) ]]; then
                echo "所输入的仓库名不符合标识符规则(字母打头，字母数字若干)"
                return 1
            fi

            local repo_path
            for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                if [ "$(database:global_array_get repo_name $i)" == "$repo_name" ]; then
                    repo_path="$(database:global_array_get repo_path $i)"
                    break
                fi
            done

            if [ "$repo_path" == "" ]; then
                echo "找不到要设置的仓库"
                return 1
            fi

            if $dynamic_cmd_config_repo_product_need; then
                local repo_product
                if [ "${dynamic_cmd_config_repo_product}" != "" ]; then
                    repo_product="${repo_path}/productdefine/common/products/${dynamic_cmd_config_repo_product}.json"
                else
                    repo_product=$(zenity:file_selector "请选择一个产品" "${repo_path}/productdefine/common/products/Hi3516DV300.json")
                    if [ "$repo_product" == "" ]; then
                        echo "选择产品中断"
                        return 1
                    fi
                fi

                if ! [ -f "$repo_product" ] || [ "$(dirname "$repo_product")" != "${repo_path}/productdefine/common/products" ]; then
                    echo "所选择的产品不存在 $repo_product"
                    echo "可以选择的如下:"
                    for file in $(ls "${repo_path}/productdefine/common/products"); do
                        echo ${file::-5}
                    done
                    return 1
                fi

                repo_product="${repo_product//*products\//}"
                repo_product="${repo_product//.json/}"

                for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                    if [ "$(database:global_array_get repo_name $i)" == "$repo_name" ]; then
                        database:global_array_set repo_product $i $repo_product
                        database:global_save
                        return 0
                    fi
                done
            fi
            ;;
        *)
            printf "$(cctl c)%-15s $(cctl g)%-15s $(cctl y)%s$(cctl 0)\n" repo_name repo_product repo_path
            for (( i = 0; i < $(database:global_get repo_number); i++ )); do
                local repo_name=$(database:global_array_get repo_name $i)
                local repo_path=$(database:global_array_get repo_path $i)
                local repo_product=$(database:global_array_get repo_product $i)
                printf "$(cctl c)%-15s $(cctl g)%-15s $(cctl y)%s$(cctl 0)\n" \
                    "$repo_name" \
                    "$repo_product" \
                    "$repo_path"
            done
            ;;
    esac
}
